﻿using System.Collections.Generic;
using System.Linq;
using S7PlcDataCollector.Database.SQLite;

namespace S7PlcDataCollector.Service.SQLiteProxy.Dal
{
    internal class S7PlcEntity
    {
        private static readonly string TableName = SqliteHelper.TableS7PlcEntity;

        /// <summary>
        /// 添加PLC实体
        /// </summary>
        /// <param name="model">PLC实体</param>
        /// <returns>Rid</returns>
        public int Insert(Models.S7PlcEntity model)
        {
            if (model == null)
            {
                return 0;
            }

            var sql = $"insert into {TableName}(Type, Ip, Port, Rack, Slot) " +
                      "values(@Type, @Ip, @Port, @Rack, @Slot);";
            return SqliteDbHelper.Insert(sql, model);
        }

        /// <summary>
        /// 批量添加PLC实体
        /// </summary>
        /// <param name="models">PLC实体集合</param>
        /// <returns>true成功，false失败</returns>
        public bool Insert(List<Models.S7PlcEntity> models)
        {
            if (models == null || models.Count == 0)
            {
                return false;
            }

            var sql = $"insert into {TableName}(Type, Ip, Port, Rack, Slot) " +
                      "values(@Type, @Ip, @Port, @Rack, @Slot);";
            return SqliteDbHelper.Insert(sql, models.ToList<object>());
        }

        /// <summary>
        /// 更新PLC实体(不修改PLC实体Id)
        /// </summary>
        /// <param name="model">PLC实体</param>
        /// <returns>true成功，false失败</returns>
        public bool Update(Models.S7PlcEntity model)
        {
            if (model == null)
            {
                return false;
            }

            var sql = $"update {TableName} " +
                      "set Type = @Type, Ip = @Ip, Port = @Port, " +
                      "    Rack = @Rack, Slot = @Slot " +
                      "where Rid = @Rid;";
            return SqliteDbHelper.Update(sql, model);
        }

        /// <summary>
        /// 删除PLC实体
        /// </summary>
        /// <param name="model">PLC实体</param>
        /// <returns>true成功，false失败</returns>
        public bool Delete(Models.S7PlcEntity model)
        {
            if (model == null)
            {
                return false;
            }

            //级联删除
            var sql = "PRAGMA foreign_keys=ON;" +
                      $"delete from {TableName} where Rid = @Rid;";
            return SqliteDbHelper.Delete(sql, model);
        }

        /// <summary>
        /// 查询PLC实体
        /// </summary>
        /// <param name="rid">PLC实体Id</param>
        /// <returns>PLC实体</returns>
        public Models.S7PlcEntity GetModel(int rid)
        {
            var sql = $"select * from {TableName} where Rid = @Rid";
            var model = new Models.S7PlcEntity() { Rid = rid };
            return SqliteDbHelper.GetDataList<Models.S7PlcEntity>(sql, model).FirstOrDefault();
        }

        /// <summary>
        /// 查询所有PLC实体
        /// </summary>
        /// <returns>PLC实体集合</returns>
        public List<Models.S7PlcEntity> GetAllModels()
        {
            var sql = $"select * from {TableName}";
            return SqliteDbHelper.GetDataList<Models.S7PlcEntity>(sql);
        }
    }
}
